﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.IO;

namespace _0712518_0712510_LTGame
{
    abstract public class Character
    {
        public float hp;
        public float mp;
        public float speed;
        public List<Animation> movement;
        public Vector2 position;
        public int faceDirection;
        public Sprite currentState;
        public int currentMove;
        public int onGround;
        public Vector2 pos2;
        public int status = 0;
        public State behavior;
        public int oldMove;
        public State oldState;
        public Character()
        {
        }
        public virtual void LoadContent(ContentManager Content)
        {
        }
        public virtual void UnloadContent()
        {
        }
        public virtual void Update(GameTime gameTime)
        {
        }
        public virtual void Draw(SpriteBatch spriteBatch)
        {
        }
        public void unLock(string name)
        {
            for (int i = 0; i < movement.Count; i++)
            {
                if (movement[i].name == name)
                {
                    if (movement[i].canUse == false)
                    {
                        movement[i].canUse = true;
                    }
                    else
                    {
                        movement[i].canUse = true;
                    }
                }
            }
        }
        public void initSkill(string filename, ContentManager Content, string image)
        {
            Animation temp = new Animation();
            try
            {

                FileStream file = new FileStream(filename, FileMode.Open);
                StreamReader reader = new StreamReader(file, Encoding.UTF8);

                string name = reader.ReadLine();
                int amount = int.Parse(reader.ReadLine());
                string loopS = reader.ReadLine();
                string[] loop = loopS.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                temp.startLoop = int.Parse(loop[0]);
                temp.endLoop = int.Parse(loop[1]);
                string time = reader.ReadLine();
                string[] timedetail = time.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                temp.timeSinceLastFrame = int.Parse(timedetail[0]);
                temp.timePerFrame = int.Parse(timedetail[1]);
                string cancel = reader.ReadLine();
                string[] canceldetail = cancel.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                temp.canCancel = int.Parse(canceldetail[0]);
                if (temp.canCancel > 0)
                {
                    temp.frameCancel = int.Parse(canceldetail[1]);
                    temp.nameSkill = new string[temp.canCancel];
                    for (int i = 0; i < temp.canCancel; i++)
                    {
                        string cancelskill = reader.ReadLine();
                        temp.nameSkill[i] = cancelskill;
                    }
                }
                temp.skill = new Sprite[amount];
                temp.spriteCount = amount;
                temp.name = name;
                temp.canUse = false;
                temp.isActive = false;
                temp.m_Texture2D = Content.Load<Texture2D>(image);

                for (int i = 0; i < amount; i++)
                {
                    string Line = reader.ReadLine();
                    string[] detail = Line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    temp.skill[i] = new Sprite();
                    temp.skill[i].start_position.X = float.Parse(detail[0]);
                    temp.skill[i].start_position.Y = float.Parse(detail[1]);
                    temp.skill[i].size.X = int.Parse(detail[2]);
                    temp.skill[i].size.Y = int.Parse(detail[3]);
                    temp.skill[i].position2draw.X = int.Parse(detail[4]);
                    temp.skill[i].position2draw.Y = int.Parse(detail[5]);
                    temp.skill[i].conclision.X = int.Parse(detail[6]);
                    temp.skill[i].conclision.Y = int.Parse(detail[7]);
                    temp.skill[i].conclisionSize.X = int.Parse(detail[8]);
                    temp.skill[i].conclisionSize.Y = int.Parse(detail[9]);
                    temp.skill[i].healthHitbox.X = int.Parse(detail[10]);
                    temp.skill[i].healthHitbox.Y = int.Parse(detail[11]);
                    temp.skill[i].healthHitboxSize.X = int.Parse(detail[12]);
                    temp.skill[i].healthHitboxSize.Y = int.Parse(detail[13]);
                    temp.skill[i].actackHitbox.X = int.Parse(detail[14]);
                    temp.skill[i].actackHitbox.Y = int.Parse(detail[15]);
                    temp.skill[i].actackHitboxSize.X = int.Parse(detail[16]);
                    temp.skill[i].actackHitboxSize.Y = int.Parse(detail[17]);


                }
                movement.Add(temp);

            }
            catch
            {
                Console.WriteLine("Error");
            }
        }
        public int SetCurrentMove(string name)
        {
            for (int i = 0; i < movement.Count; i++)
            {
                if (movement[i].name == name)
                {
                    return i;
                }

            }
            return -1;
        }
        public void SetAnimation(string name)
        {
            // movement[currentMove].isActive = false;
            if (movement[currentMove].name != name)
            {
                movement[currentMove].isActive = false;
            }
            for (int i = 0; i < movement.Count; i++)
            {
                if (movement[i].name == name)
                {
                    if (movement[i].canUse == true)
                    {
                        if (movement[i].isActive == true)
                        {
                            movement[i].currentFrame++;
                            if (movement[i].currentFrame > movement[i].endLoop)
                            {
                                movement[i].currentFrame = movement[i].startLoop;
                            }
                        }
                        else
                        {
                            movement[i].isActive = true;
                            movement[i].currentFrame = 0;
                            
                        }
                        currentMove = i;
                        currentState.setSprite(movement[i].skill[movement[i].currentFrame]);
                    }

                }

            }
        }
        public void SetAnimation(string name, int start, int end)
        {
           
            // movement[currentMove].isActive = false;
            if (movement[currentMove].name != name)
            {
                movement[currentMove].isActive = false;
            }
            for (int i = 0; i < movement.Count; i++)
            {
                if (movement[i].name == name)
                {
                    if (movement[i].canUse == true)
                    {
                        if (movement[i].isActive == true)
                        {
       
                            movement[i].currentFrame++;
                                    if(movement[i].currentFrame > end)
                                    {
                                        movement[i].currentFrame = end;
                                    }
                                
                               
                            
                        }
                        else
                        {
                            movement[i].isActive = true;
                            movement[i].currentFrame = start;
                            
                        }
                        currentMove = i;
                        currentState.setSprite(movement[i].skill[movement[i].currentFrame]);
                    }

                }

            }

        }

    }
}
